package cn.tedu.order.service.impl;

import cn.tedu.order.feign.CartClient;
import cn.tedu.order.feign.StockClient;
import cn.tedu.order.mapper.OrderMapper;
import cn.tedu.order.service.OrderService;
import cn.tedu.pojo.param.OrderAddParam;
import cn.tedu.pojo.po.OrderPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    //调用mapper中的新增方法
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StockClient stockClient;
    @Autowired
    private CartClient cartClient;

    @Override
    public Boolean addOrder(OrderAddParam orderAddParam) {
        //1.减少库存   orderAddParam.getProductCode  orderAddParam.getCount
        stockClient.reduceStock(orderAddParam.getProductCode(), orderAddParam.getCount());
        //2.新增订单到数据库中
        //需要将orderAddParam，转变为OrderPO
        OrderPO orderPO = new OrderPO();
        BeanUtils.copyProperties(orderAddParam, orderPO);
        int orderResult = orderMapper.insert(orderPO);
        if (!(orderResult >= 1)) {
            return false;
        }
        //3.删除对应用户购物车中选中的商品 orderAddParam.getProductCode   orderAddParam.getUserId
        cartClient.deleteCart(orderAddParam.getProductCode(), orderAddParam.getUserId());
        return true;
    }
}
